﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using AAPL.Common;
using AAPL.Common.DataShapes;

namespace AAPL.BAL.Persistence.Persisters
{
    public class CategoryPersister
    {


        public void Save(Category category)
        {
            if (category.CategoryGuid == NullValues.NullGuid)
            {
                Insert(category);
            }
            else
            {
                Update(category);
            }
        }


        public void Insert(Category dto)
        {
            if (dto.CategoryGuid == NullValues.NullGuid) { dto.CategoryGuid = Guid.NewGuid(); }
            SqlDao dao = new SqlDao();
            string sql = @"INSERT INTO [dbo].[Category]
                               ([CategoryGuid]
                               ,[CategoryName]
                               ,[CategoryKey])
                         VALUES
                               (@CategoryGuid
                               ,@CategoryName
                               ,@CategoryKey)";
            SqlCommand command = dao.GetSqlCommand(sql);
            command.Parameters.Add(dao.CreateParameter("@CategoryGuid", dto.CategoryGuid));
            command.Parameters.Add(dao.CreateParameter("@CateogoryName", dto.CategoryName, 50));
            command.Parameters.Add(dao.CreateParameter("@CateogoryName", dto.CategoryKey, 50));
            dao.ExecuteNonQuery(command);
        }


        public void Update(Category dto)
        {
            SqlDao dao = new SqlDao();
            string sql = @"UPDATE [dbo].[Category]
                           SET [CategoryName] = @CategoryName
                           ,[CategoryKey] = @CategoryKey
                           WHERE CategoryGuid = @CategoryGuid ";

            SqlCommand command = dao.GetSqlCommand(sql);
            command.Parameters.Add(dao.CreateParameter("@CategoryGuid", dto.CategoryGuid));
            command.Parameters.Add(dao.CreateParameter("@CateogoryName", dto.CategoryName, 50));
            command.Parameters.Add(dao.CreateParameter("@CateogoryName", dto.CategoryKey, 50));
            dao.ExecuteNonQuery(command);
        }


        public void Delete(Category category)
        {
            SqlDao dao = new SqlDao();
            string sql = @"DELETE FROM [dbo].[Category]
                           WHERE CategoryGuid = @CategoryGuid ";
            SqlCommand command = dao.GetSqlCommand(sql);
            command.Parameters.Add(dao.CreateParameter("@CategoryGuid", category.CategoryGuid));
            dao.ExecuteNonQuery(command);
        }


        public void InsertPostCategory(Guid postGuid, Category category)
        {
            SqlDao dao = new SqlDao();
            string sql = @"INSERT INTO [dbo].[PostCategory]
                               ([PostGuid]
                               ,[CategoryGuid])
                         VALUES
                               (@PostGuid
                               ,@CategoryGuid)";
            SqlCommand command = dao.GetSqlCommand(sql);
            command.Parameters.Add(dao.CreateParameter("@PostGuid", postGuid));
            command.Parameters.Add(dao.CreateParameter("@CategoryGuid", category.CategoryGuid));
            dao.ExecuteNonQuery(command);
        }

    }
}
